package com.jmyd.sharding.server.controller;

import com.jmyd.commons.base.dto.KeywordQueryDto;
import com.jmyd.commons.base.vo.PageBean;
import com.jmyd.commons.base.vo.Result;
import com.jmyd.commons.constants.BaseUrlConstants;
import com.jmyd.sharding.server.domain.dto.AddressDto;
import com.jmyd.sharding.server.domain.vo.AddressVo;
import com.jmyd.sharding.server.service.IAddressService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 地址管理相关接口
 *
 * @author samphin
 * @date 2019-8-28 16:24:10
 */
@RestController
@RequestMapping(BaseUrlConstants.BASE_ADMIN_PREFIX + "/address")
public class AddressController {

    @Resource
    private IAddressService addressService;

    /**
     * 创建地址信息
     *
     * @param dto
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody @Validated AddressDto dto) {
        boolean bl = this.addressService.save(dto);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 修改地址信息
     *
     * @param dto
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody @Validated AddressDto dto) {
        boolean bl = this.addressService.save(dto);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 删除地址信息
     *
     * @param id 地址id
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable Long id) {
        boolean bl = this.addressService.deleteById(id);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 查询地址分页列表
     *
     * @param queryDto 查询条件
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @GetMapping
    public Result<PageBean<AddressVo>> queryPageList(KeywordQueryDto queryDto) {
        PageBean<AddressVo> pageBean = this.addressService.queryPageList(queryDto);
        return new Result<>(pageBean);
    }

    /**
     * 查询地址详情
     *
     * @param id 地址ID
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @GetMapping("/{id}")
    public Result<AddressVo> queryById(@PathVariable Long id) {
        AddressVo vo = this.addressService.queryById(id);
        return new Result<>(vo);
    }
}
